<template>
  <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item label="物料编码" prop="goodsCode">
          <el-input v-model="queryParams.code" placeholder="请输入物料编码"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入物料名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="物料型号" prop="model">
          <el-input v-model="queryParams.model" placeholder="请输入物料型号"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-table border v-loading="loading" :data="columns" ref="dragTable">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="物料编码" align="center" prop="goodsCode"/>
          <el-table-column label="物料名称" align="center" prop="goodsName"/>
          <el-table-column label="物料型号" align="center" prop="goodModel"/>
          <el-table-column label="计量单位" align="center" prop="goodsUnit"/>
          <el-table-column label="数量" align="center" prop="goodsCount" width="200">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.goodsCount" :min="0" :precision="3" size="mini"></el-input-number>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList"/>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitPleaseBuy" v-if="">提 交 请 购</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {deleteAction, downloadFile, getAction, addAction} from '@/api/manage'

export default {
  name: "GoodsSaleMaterialPleaseBuyList",
  dicts: [],
  components: {},
  data() {
    return {
      title: "材料请购",
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      rows: [],
      columns: [],
      rules: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售信息管理表格数据
      dataList: [],
      form: {},
      gsaleId: null,
      ironRecordId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        mainId: null,
        name: null,
        code: null,
        brand: null,
        unit: null,
        costPrice: null,
        count: null,
        cost: null,
        validityTerm: null,
        buyId: null,
        buyName: null,
        buyDate: null,
        buyType: null,
        reimburse: null,
        supplierId: null,
        supplierName: null,
        dateRange: "",
        params: {},
      },
      url: {
        list: '/goodsSaleMaterial/goodsSaleMaterial/goodsSaleMaterialPleaseBuyList',
        pleaseBuyForMaterial: '/goodsSaledo/goodsSaledo/pleaseBuyForMaterial',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    // this.intDictsForPrj();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight);
  },
  methods: {
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    pleaseBuyByIronRecord(ironRecordId) {
      this.queryParams.ironRecordId = ironRecordId;
      this.ironRecordId = ironRecordId;
      this.open = true;
      this.getList();
    },

    submitPleaseBuy() {
      let that = this;
      let arrays = that.columns;
      for (let i = 0; i < arrays.length; i++) {
        arrays[i].pleaseBuyCount = arrays[i].goodsCount;
      }
      that.$refs["form"].validate(valid => {
        if (valid) {
          const params = Object.assign({});
          params.ironRecordId = that.ironRecordId;
          params.columns = that.columns;
          addAction(that.url.pleaseBuyForMaterial, params).then(response => {
            that.$modal.msgSuccess("添加成功！");
            that.open = false;
            that.$emit('ok');
          });
        }
      });
    },


    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 查询销售信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  }
};
</script>
